//
//  CategoryPlayViewController.m
//  JZMCFM
//
//  Created by MJL on 15/11/4.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "CategoryPlayViewController.h"
#import "CategoryPlayListModel.h"
#import "PlayListModel.h"
#import "AudioPlayer.h"
#import "MJLPlayListHelper.h"
@interface CategoryPlayViewController ()<UITableViewDataSource,UITableViewDelegate,AudioPlayerDelegate>
@property (strong, nonatomic) IBOutlet UITableView *PlayTableView;
@property(nonatomic,strong)PlayListModel *listModel;


//滑块
@property(nonatomic,strong)UISlider *slider;
//上一曲
@property(nonatomic,strong)UIButton *lastBtn;
//播放
@property(nonatomic,strong)UIButton *playBtn;
//下一曲
@property(nonatomic,strong)UIButton *nextBtn;
//图片
@property(nonatomic,strong)UIImageView *imageViewc;

#pragma mark 开始播放
- (void)startPlay;
#pragma mark button的标志
@property(nonatomic,assign)BOOL *isClicked;

@end

@implementation CategoryPlayViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //开始播放
    [self startPlay];
    [self UpdateUI];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //title
    self.listModel = self.playListArray[self.index];
    self.title = self.listModel.title;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"mjliconfont-xiangxiajiantou"] style:UIBarButtonItemStylePlain target:self action:@selector(btnAction:)];
    
    //设置代理
    
    self.PlayTableView.dataSource = self;
    self.PlayTableView.delegate = self;

    [self.PlayTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"flag"];
    
//    [[MJLPlayListHelper sharedInstance] downLoadZTDataWithString:[NSString stringWithFormat:@"%@8939178?albumId=%ld&device=iPhone&trackUid=1266964",ZTDataUrr,self.copyID] withBlock:^{
//        [self.PlayTableView reloadData];
//    }];
    
    
}

#pragma mark 更新UI
- (void)UpdateUI
{
    self.listModel = self.playListArray[self.index];
    self.title = self.listModel.title;

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
    UIImageView *lagerImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    
    [lagerImage sd_setImageWithURL:[NSURL URLWithString:self.listModel.albumImage]];
    UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    UIVisualEffectView *lagerView = [[UIVisualEffectView alloc]initWithEffect:beffect];
    lagerView.frame = lagerImage.bounds;
    [lagerImage addSubview:lagerView];
    lagerImage.backgroundColor = [UIColor grayColor];
    [view addSubview:lagerImage];
    
    //图片
    UIImageView *imageViewc = [[UIImageView alloc]initWithFrame:CGRectMake(80, 20,190,190)];
    if ([self.listModel.coverLarge isEqualToString:@""]) {
        [imageViewc sd_setImageWithURL:[NSURL URLWithString:self.listHeaderModel.coverLarge]];
    }else{
    [imageViewc sd_setImageWithURL:[NSURL URLWithString:self.listModel.coverMiddle]];
    }
    imageViewc.layer.cornerRadius = 95;
    imageViewc.clipsToBounds = YES;
    self.imageViewc = imageViewc;
    [view addSubview:imageViewc];
    
    //滑块
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageViewc.frame)+20, [UIScreen mainScreen].bounds.size.width, 20)];
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    self.slider = slider;
    self.slider.maximumValue = self.listModel.duration;
    [view addSubview:slider];
    
    //上一首
    UIButton *lastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [lastBtn setBackgroundImage:[UIImage imageNamed:@"mjliconfont-ttpodicon"] forState:UIControlStateNormal];
    lastBtn.frame = CGRectMake(100, CGRectGetMaxY(slider.frame)+10, 30, 30);
    
    [lastBtn addTarget:self action:@selector(lastBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.lastBtn = lastBtn;
    
    [view addSubview:lastBtn];
    
    //播放/暂停
    UIButton*playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    playBtn.frame = CGRectMake(CGRectGetMaxX(lastBtn.frame) + 30, CGRectGetMinY(lastBtn.frame), 30, 30);
    [playBtn setBackgroundImage:[UIImage imageNamed:@"mjliconfont-iconfont32"] forState:UIControlStateNormal];
    [playBtn addTarget:self action:@selector(playBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.playBtn = playBtn;
    
    [view addSubview:self.playBtn];
    
    //下一首
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"mjliconfont-ttpodicon(1)"] forState:UIControlStateNormal];
    nextBtn.frame = CGRectMake(CGRectGetMaxX(playBtn.frame)+30, CGRectGetMinY(playBtn.frame), 30, 30);
    [nextBtn addTarget:self action:@selector(nextButton:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:nextBtn];
    self.nextBtn = nextBtn;
    self.PlayTableView.tableHeaderView = view;
    
}

#pragma mark 单例事件
+ (instancetype)sharedInstance
{
    static CategoryPlayViewController *share = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[CategoryPlayViewController alloc]init];
    });
    return share;
}

- (void)btnAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 代理事件
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flag"];
    cell.textLabel.text = @"lalal";
    return cell;
}

#pragma mark 自定义头部

#pragma mark 实现代理方法
- (void)playWithPlayer:(AudioPlayer *)player withProgress:(CGFloat)progress
{
    self.slider.value = progress;
    if ([AudioPlayer sharedInstance].isPlaying == YES) {
        self.imageViewc.transform = CGAffineTransformRotate(self.imageViewc.transform, M_1_PI/180);
    }else{
        self.imageViewc.transform = CGAffineTransformRotate(self.imageViewc.transform, 0);
    }
}

- (void)playEndFinish:(AudioPlayer *)palyer
{
    self.index ++;
    if (self.index > self.playListArray.count) {
        self.index = 0;
    }
    [self startPlay];
    [self UpdateUI];
}


#pragma mark 滑块
- (void)sliderAction:(UISlider *)sender
{
    [[AudioPlayer sharedInstance]seekToTime:sender.value];
}

#pragma mark 开始播放
- (void)startPlay
{
    
    PlayListModel *model = self.playListArray[self.index];
    [self UpdateUI];
    [[AudioPlayer sharedInstance]prepareWithUrl:model.playUrl64];
    [AudioPlayer sharedInstance].delegate = self;
}


#pragma 上一首
- (void)lastBtnAction:(UIButton *)sender
{
    self.index --;
    if (self.index < 0) {
        self.index = self.playListArray.count - 1;
    }
    [self startPlay];
    [self UpdateUI];
}

#pragma mark 播放
- (void)playBtnAction:(UIButton *)sender
{
    if ([AudioPlayer sharedInstance].isPlaying == YES) {
        [self.playBtn setBackgroundImage:[UIImage imageNamed:@"mjliconfont-bofang"] forState:UIControlStateNormal];
        //暂停
        [[AudioPlayer sharedInstance] pause];
    }else{
        [self.playBtn setBackgroundImage:[UIImage imageNamed:@"mjliconfont-iconfont32"] forState:UIControlStateNormal];
        [[AudioPlayer sharedInstance]play];
    }
}

- (void)nextButton:(UIButton *)sender
{
    self.index ++;
    if (self.index >= self.playListArray.count) {
        self.index = 0;
    }
    [self startPlay];
    [self UpdateUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
